package com.freedom.start.sys.controller;

import com.freedom.start.common.Result;
import com.freedom.start.sys.entity.OrgEntity;
import com.freedom.start.sys.service.AsyncService;
import com.freedom.start.sys.service.OrgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhenggang.liu
 * @Date: 2020/1/18 9:57
 */
@Controller
@RequestMapping("/sys/org")
public class OrgController {

    @Autowired
    OrgService orgService;
    @Autowired
    AsyncService asyncService;

    @ModelAttribute
    public OrgEntity get(String id) {
        if (StringUtils.isNotBlank(id)) {
            return orgService.get(id);
        }
        return new OrgEntity();
    }

    /**
     * 返回list页面
     *
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "sys/org/list";
    }

    /**
     * 返回增加或是修改页面
     *
     * @return
     */
    @RequestMapping("/form")
    public String form() {
        return "sys/org/form";
    }

    /**
     * 保存
     *
     * @param orgEntity 前台传入的属性封装到此对象中
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(@Valid OrgEntity orgEntity) {
        //是否是编辑
        boolean isEdit = orgEntity.getId() != null && orgEntity.getId().length() > 0;
        //获取老机构，数据库中还未改变的数据
        OrgEntity oldOrg = orgService.get(orgEntity.getId());
        try {
            orgService.save(orgEntity);
            if (isEdit) {
                int oldLevel = oldOrg.getLevel();
                String oldOrgIds = oldOrg.getOrgIds();
                //机构层级或是父机构ids变化才会更新子孙机构的属性
                if (oldLevel != orgEntity.getLevel() || !oldOrgIds.equalsIgnoreCase(orgEntity.getOrgIds())) {
                    //为异步程序，以优化用户体验
                    asyncService.updateOrgParentIds(orgEntity);
                }
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("success");
    }

    /**
     * 返回json对象，供easyui使用
     *
     * @param request   这里从中获取分页信息
     * @param orgEntity 封装搜索条件
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dataList")
    public Map<String, Object> dataList(HttpServletRequest request, OrgEntity orgEntity) {
        Map<String, Object> pageForEasyUI = orgService.findPageForEasyUI(request, orgEntity);
        return pageForEasyUI;
    }

    /**
     * 返回所有记录，combobox中使用
     *
     * @param orgEntity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/all")
    public List<OrgEntity> all(OrgEntity orgEntity) {
        List<OrgEntity> all = orgService.findAll(orgEntity);
        return all;
    }

    /**
     * 删除，支持批量
     *
     * @param ids 记录的id，多条用“,”分割
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public Result delete(String ids) {
        try {
            orgService.deleteBatch(ids);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("删除成功");
    }
}
